﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Word = Microsoft.Office.Interop.Word;

using System.IO;

using NLog;


namespace OpenDoPE_Wed
{
    class OpenXmlUtils
    {
        static Logger log = LogManager.GetLogger("OpenXmlUtils");

        public static string getDocumentXml(Word.Document workingWordDocument)
        {
            //Get stream for the range. This is the System.IO.Packaging.Package stream
            Stream packageStream;
            try
            {
                packageStream = workingWordDocument.Range().GetPackageStreamFromRange();
            }
            catch (Exception e)
            {
                log.Error(e);
                System.Windows.Forms.MessageBox.Show(e.Message);
                return null;
            }

            //Use Open Xml SDK to process it.
            WordprocessingDocument workingDoc = WordprocessingDocument.Open(packageStream, true);

            return workingDoc.MainDocumentPart.Document.OuterXml;

        }

    }
}
